#include "CollapsePanel.h"

CollapsePanel::CollapsePanel(QWidget *parent ) :
    QWidget(parent)
{
    init();
    this->tb_ac->setToolButtonStyle(Qt::ToolButtonFollowStyle);
    setChecked(true);
    initSignals();
}


void CollapsePanel::init()
{
    verticalLayout = new QVBoxLayout;
    verticalLayout->setSpacing(0);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    tb_ac = new QToolButton(this);
    tb_ac->setObjectName(QString::fromUtf8("tb_ac"));
    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(tb_ac->sizePolicy().hasHeightForWidth());
    tb_ac->setSizePolicy(sizePolicy);
    QIcon icon;
    icon.addFile(QString::fromUtf8(":/res/icons/Down-3.svg"), QSize(), QIcon::Normal, QIcon::Off);
    icon.addFile(QString::fromUtf8(":/res/icons/Right-3.svg"), QSize(), QIcon::Normal, QIcon::On);
    icon.addFile(QString::fromUtf8(":/res/icons/Right-3.svg"), QSize(), QIcon::Active, QIcon::Off);
    tb_ac->setIcon(icon);
    tb_ac->setIconSize(QSize(24, 24));
    tb_ac->setCheckable(true);
    tb_ac->setMaximumHeight(40);

    verticalLayout->addWidget(tb_ac);
    verticalSpacer = new QSpacerItem(20, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->insertItem(2,verticalSpacer);
    this->setLayout(verticalLayout);

    // verticalLayout->setStretch(1, 1);
}



void CollapsePanel::setChecked(bool checked)
{
    this->tb_ac->setChecked(checked);
    if(userWidget.isNull()) return;
    if(checked){
        this->userWidget->show();
    }else{
        this->userWidget->hide();
    }
}

void CollapsePanel::initSignals()
{
    connect(tb_ac,&QToolButton::toggled,this,&CollapsePanel::on_tb_ac_toggled);
}

void CollapsePanel::setUserWidget(QWidget *f)
{
    userWidget.reset(f);
    verticalLayout->insertWidget(1,f,1);

}

QString CollapsePanel::getTitle() const
{
    return tb_ac->text();
}

void CollapsePanel::setTitle(const QString val)
{
    tb_ac->setText(val);
}

bool CollapsePanel::getChecked() const
{
    return tb_ac->isChecked();
}

void CollapsePanel::on_tb_ac_toggled(bool checked)
{
    if(userWidget.isNull()) return;
    if(checked){
        this->userWidget->show();
    }else{
        this->userWidget->hide();
    }
}

